package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;

//地区资源控制器
@RestController
@RequestMapping("regions")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private ServletContext servletContext;

    @RequestMapping(method = RequestMethod.GET)
    public List<Region> listAll(Integer state) {
        return regionService.listAll(state);
    }


    //accept  前台期望接收什么类型的(html或者json)数据

    /**
     * 根据地区id查询攻略
     *
     * @param
     * @return
     */
    //参数和qo的字段名字一样,自动封装进StrategyQueryObject对象
    //设置请求头produces,专门生产html           text/html;charset=utf-8   html
    @RequestMapping(value = "/{placeId}/strategies", method = RequestMethod.GET, produces = "text/html;charset=utf-8")
    public void queryStrategies(StrategyQueryObject qo, HttpServletResponse response) throws Exception {
        //查询全部攻略
        //qo.setPageSize(8);//分页
        qo.setState(null);//不需要条件
        PageInfo all = strategyService.query(qo);

        //查询推荐攻略
        qo.setState(Strategy.STATE_HOT);//推荐
        qo.setPageSize(0);//不分页,设置为0

        PageInfo hot = strategyService.query(qo);


        //使用freeMarker渲染模板
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
        //设置模板目录   绝对路径
        configuration.setDirectoryForTemplateLoading(new File(servletContext.getRealPath("/template")));
        //获取模板   文件和编码类型
        Template template = configuration.getTemplate("strategyTemplate.ftl", "utf-8");
        //数据
        HashMap<Object, Object> map = new HashMap<>();
        map.put("hot", hot);
        map.put("all", all);

        //渲染数据    传入map对象和输入流进行渲染
        template.process(map, response.getWriter());

    }

    /**
     * 根据地区id查询攻略(json用于滚动页面)
     *
     * @param
     * @return
     */
    //参数和qo的字段名字一样,自动封装进StrategyQueryObject对象

    //设置请求头,专门生产json            application/json;charset=utf-8   json
    @RequestMapping(value = "/{placeId}/strategies", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public PageInfo queryStrategiesJson(StrategyQueryObject qo) throws Exception {

        return strategyService.query(qo);
    }
}

